package net.xujialiang.XTestRPA.Controller.TenantManager;

import net.xujialiang.XTestRPA.Config.SpringSecurityConfig.SecurityUser;
import net.xujialiang.XTestRPA.Entity.XRPALog;
import net.xujialiang.XTestRPA.Model.ResultData;
import net.xujialiang.XTestRPA.Service.XRPALogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static net.xujialiang.XTestRPA.Enum.ReturnCode.RC999;

@RequestMapping("/api/tenant")
@RestController
public class OpLog {

    @Autowired
    XRPALogService logService;

    @RequestMapping(value = "/fetchLog", method = RequestMethod.POST)
    @ResponseBody
    public ResultData create(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        Integer page = (Integer) reqBody.get("page");
        Integer pageSize = (Integer) reqBody.get("pageSize");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
//        String opAccount = sUser.getUsername();
        String module = (String) reqBody.get("module");
        String opAccount = (String) reqBody.get("account");
        try {
            Map<String, Object> ret = logService.fetch(module, ttc, opAccount, page, pageSize);
            return ResultData.success(ret);
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }
}
